// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef OpenMeca_Gui_UserItemDialogInterface_hpp
#define OpenMeca_Gui_UserItemDialogInterface_hpp

#include <iostream>
#include <QAction>

#include "OpenMeca/Gui/UserItemDialog.hpp"
#include "OpenMeca/Gui/MainWindow.hpp"
#include "OpenMeca/Core/AutoRegister.hpp"
#include "OpenMeca/Core/Drawable.hpp"

namespace OpenMeca
{
  namespace Gui
  {
    template <class T>
    class UserItemDialogInterface : public UserItemDialog
   {

   public:
     void New(QAction& action);
     template<class Parent> void New(QAction& action, Parent& parent);
     void Edit(QAction& action, T& item);
      
   protected:
     UserItemDialogInterface();
     virtual ~ UserItemDialogInterface();
     Core::UserItem& GetCurrentUserItem();
     T& GetCurrentItem();
     bool IsCurrentUserItemStillExist();
     
   private:
     void Reset();

    private:
     T* currentItem_;
      
    };

    template <class T>
    inline
    UserItemDialogInterface<T>::UserItemDialogInterface()
      :UserItemDialog(),
       currentItem_(0)
    {
    }
    
    template <class T>
    inline
    UserItemDialogInterface<T>::~UserItemDialogInterface()
    {
    }
    
    template <class T>
    inline void
    UserItemDialogInterface<T>::New(QAction& action)
    {
      Dialog::Show_CallBack();
      UserItemDialog::ActiveNewItemMode();
      UserItemDialog::SetAction(action);
      assert(currentItem_ == 0);
      currentItem_ = new T();
      typename T::Dialog* me = static_cast< typename T::Dialog*>(this);
      QString title = QString("New ") + QString(T::GetStrType().c_str());
      MainWindow::Get().GetDialogContainer().insertItem(1, me, title);
      MainWindow::Get().GetDialogContainer().setCurrentIndex(1);
      Init();
      me->show();
    }

    template <class T>
    template <class Parent>
    inline void
    UserItemDialogInterface<T>::New(QAction& action, Parent& parent)
    {
      Dialog::Show_CallBack();
      UserItemDialog::ActiveNewItemMode();
      UserItemDialog::SetAction(action);
      assert(currentItem_ == 0);
      currentItem_ = new T(parent);
      typename T::Dialog* me = static_cast< typename T::Dialog*>(this);
      QString title = QString("New ") + QString(T::GetStrType().c_str());
      MainWindow::Get().GetDialogContainer().insertItem(1, me, title);
      MainWindow::Get().GetDialogContainer().setCurrentIndex(1);
      Init();
      me->show();
    }


    template <class T>
    inline void
    UserItemDialogInterface<T>::Edit(QAction& action, T& item)
    {
      Dialog::Show_CallBack();
      UserItemDialog::DisableNewItemMode();
      UserItemDialog::SetAction(action);
      assert(currentItem_ == 0);
      currentItem_ = &item;
      typename T::Dialog* me = static_cast< typename T::Dialog*>(this);
      QString title = QString("Edit ") + QString(T::GetStrType().c_str());
      MainWindow::Get().GetDialogContainer().insertItem(1, me, title);
      MainWindow::Get().GetDialogContainer().setCurrentIndex(1);
      Init();
      me->show();
    }

    template <class T>
    inline bool
    UserItemDialogInterface<T>::IsCurrentUserItemStillExist()
    {
      assert(currentItem_!=0);
      return Core::AutoRegister<T>::Exist(*currentItem_);
    }
    
    template <class T>
    inline Core::UserItem& 
    UserItemDialogInterface<T>::GetCurrentUserItem()
    {
      assert(currentItem_!=0);
      return reinterpret_cast<Core::UserItem& >(*currentItem_);
    }

    template <class T>
    inline T& 
    UserItemDialogInterface<T>::GetCurrentItem()
    {
      assert(currentItem_!=0);
      return *currentItem_;
    }

    template <class T>
    inline void
    UserItemDialogInterface<T>::Reset()
    {
      UserItemDialog::Reset();
      currentItem_=0;
    }

  }
}

#endif
